<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'db_connection.php';

class dao_category_journal {

    //Methods
    public function __construct() {
        
    }

    public function get_all_order_by_journal_id() {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_category_journal ORDER BY JournalID";
        $result = mysqli_query($con, $query) or die('Query failed: ' . mysqli_error());
        $output = array();
        while ($item = mysqli_fetch_array($result)) {
            array_push($output, $item);
        }

        $db->close_connect($con);
        return $output;
    }

    public function get_all_order_by_category_id() {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_category_journal ORDER BY CategoryID";
        $result = mysqli_query($con, $query) or die('Query failed: ' . mysqli_error());
        $output = array();
        while ($item = mysqli_fetch_array($result)) {
            array_push($output, $item);
        }

        $db->close_connect($con);
        return $output;
    }

    public function get_by_id($cat_journal_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_category_journal WHERE cat_journal_id = " . $cat_journal_id;
        $result = mysqli_query($con, $query) or die('Query failed: ' . mysqli_error());
        $output = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $output;
    }

    public function check_exist($JournalID, $CategoryID) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_category_journal WHERE JournalID = " . $JournalID . " AND CategoryID = " . $CategoryID;
        $result = mysqli_query($con, $query) or die('Query Failed: ' . mysqli_error());
        if (mysqli_num_rows($result) > 0) {
            return true;
        } else {
            return false;
        }
        $db->close_connect($con);
    }

    public function get_by_journal_id_and_category_id($JournalID, $CategoryID) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_category_journal WHERE JournalID = " . $JournalID . " AND CategoryID = " . $CategoryID;
        $result = mysqli_query($con, $query) or die('Query Failed: ' . mysqli_error());
        $output = mysqli_fetch_array($result);
        $db->close_connect($con);
        return $output;
    }
    
    public function save($cat_journal_id, $journal_id, $category_id, $description) {
        $db = new db_connection();
        $con = $db->open_connect();

        //if exist && cat journal id != 0 --> update
        if ($this->check_exist($journal_id, $category_id) && $cat_journal_id != 0) {
            $query = "UPDATE tbl_category_journal SET JournalID = " . $journal_id . ", CategoryID = " . $category_id . ", description = '" . $description . "' WHERE cat_journal_id = " . $cat_journal_id;
        } else if ($this->check_exist($journal_id, $category_id) && $cat_journal_id == 0) {
            //if exist && cat journal id == 0 --> fail
            return false;
        } else if ($cat_journal_id == 0) {
            //if cat_journal_id == 0 --> insert
            $query = "INSERT INTO tbl_category_journal(JournalID, CategoryID, description) VALUES (" . $journal_id . ", " . $category_id . ", '" . $description . "')";
            if (mysqli_query($con, $query) or die('Query Failed: ' . mysqli_error())) {
                $db->close_connect($con);
                return true;
            } else {
                $db->close_connect($con);
                return false;
            }
        }
    }

    public function edit($cat_journal_id, $journal_id, $category_id, $description) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "UPDATE tbl_category_journal SET JournalID = " . $journal_id . ", CategoryID = " . $category_id . ", description = '" . $description . "' WHERE cat_journal_id = " . $cat_journal_id;
        if (mysqli_query($con, $query) or die('Query Failed: ' . mysqli_error())) {
            $db->close_connect($con);
            return true;
        } else {
            $db->close_connect($con);
            return false;
        }
    }

    function delete($cat_journal_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_category_journal WHERE cat_journal_id = " . $cat_journal_id;
        if (mysqli_query($con, $query) or die('Query failed: ' . mysqli_error())) {
            $db->close_connect($con);
            return true;
        } else {
            $db->close_connect($con);
            return false;
        }
    }

}
